//机构课程评论点赞
exports.organization_comment_zan = function (req, res) {

    var bodyParams = req.routeInfo.bodyParams;
    //最终结果
    var resultData = {};

    var is_comment_zan = 0;

    var CommentZanData = 0;
    //任务流创建
    async.waterfall([
            //1.查询用户是否已对当前评论点赞
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganizationCommentZanRecord + "/oczr_uid/" + bodyParams.u_id + "/oczr_ocid/" + bodyParams.oc_id,
                        method: 'POST',
                        data: {
                            fields: ["oczr_id", "is_delete"].join(",")
                        }
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        is_comment_zan = data.data;
                        cb();
                    }
                );

            },
            function (cb) {
                //已点
                if (is_comment_zan.length !== 0) {
                    if (is_comment_zan[0].is_delete <= 0) {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.updateOrganizationCommentZanRecord,
                                method: 'POST',
                                data: {
                                    oczr_id: is_comment_zan[0].oczr_id,
                                    is_delete: 1
                                }
                            },
                            function (err, data) {
                                // //console.log(data);
                                //判断是否有错误信息
                                if (err) {
                                    return RES.response(res, false, data);
                                }
                                resultData.cancleZan = "已取消点赞";
                                cb();
                            }
                        );
                    } else {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.updateOrganizationCommentZanRecord,
                                method: 'POST',
                                data: {
                                    oczr_id: is_comment_zan[0].oczr_id,
                                    is_delete: 0
                                }
                            },
                            function (err, data) {
                                // //console.log(data);
                                //判断是否有错误信息
                                if (err) {
                                    return RES.response(res, false, data);
                                }
                                resultData.reTouchZan = "点赞成功";
                                cb();
                            }
                        );
                    }
                    //未点
                } else {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.addOrganizationCommentZanRecord,
                            method: 'POST',
                            data: {
                                oczr_uid: bodyParams.u_id,
                                oczr_ocid: bodyParams.oc_id
                            }
                        },
                        function (err, data) {
                            // //console.log(data);
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }
                            // var addData = data.data;
                            //console.log(data.data);
                            cb();
                        }
                    );
                }
            },
            //查询点赞数
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_organization_comment + "/oc_id/" + bodyParams.oc_id,
                        method: 'POST',
                        data: {
                            fields: ["oc_id", "oc_zan_count"].join(",")
                        }
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return RES.response(res, false, data);
                        }

                        CommentZanData = data.data;
                        cb();
                    }
                );

            },
            //3.论坛问题回复点赞数更新
            function (cb) {
                if (is_comment_zan.length !== 0) {
                    if (is_comment_zan[0].is_delete <= 0) {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.updateOrganizationComment,
                                method: 'POST',
                                data: {
                                    oc_id: bodyParams.oc_id,
                                    oc_zan_count: parseInt(CommentZanData[0].oc_zan_count) - 1
                                }
                            },
                            function (err, data) {
                                //判断是否有错误信息
                                if (err) {
                                    return RES.response(res, false, data);
                                }

                                resultData.updateData = data.data;
                                cb();
                            }
                        );
                    } else {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.updateOrganizationComment,
                                method: 'POST',
                                data: {
                                    oc_id: bodyParams.oc_id,
                                    oc_zan_count: parseInt(CommentZanData[0].oc_zan_count) + 1
                                }
                            },
                            function (err, data) {
                                //判断是否有错误信息
                                if (err) {
                                    return RES.response(res, false, data);
                                }

                                resultData.updateData = data.data;
                                cb();
                            }
                        );
                    }
                } else {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.updateOrganizationComment,
                            method: 'POST',
                            data: {
                                oc_id: bodyParams.oc_id,
                                oc_zan_count: parseInt(CommentZanData[0].oc_zan_count) + 1
                            }
                        },
                        function (err, data) {
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }

                            resultData.updateData = data.data;
                            cb();
                        }
                    );
                }
            },
        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

}